console.log(process.cwd());

var tag_name = "";
var debug_type = true;//默认是true
var exec = require('child_process').exec;
var cmdStr = 'git pull';
var commit_id = "";

//pre1.读取启动参数
process.argv.forEach(function (val, index, array) 
{
  switch(index)
  {
	  case 2:
        let obj = JSON.parse(val);
		tag_name = obj.tag_name;
		
		if(obj.debug != null)
		{
			debug_type = obj.debug;
		}
		
        if(tag_name)
        {
            cmdStr = "git pull";
            exec(cmdStr, git_pull);
        }
        else
        {
            cmdStr = "git rev-parse HEAD";
            exec(cmdStr, get_tag_id);
        }
		break;
	  case 3:
		break;
	  case 4:
		break;
    }
  }
);

//1.git pull-->拉取到最新
function git_pull(err, stdout, stderr)
{
	if (err) {
        console.error('egret publish error:' + err);
    }
    else 
    {
        console.log(stdout);
        cmdStr = "git tag";
        exec(cmdStr, git_tag);
    }
}

function git_tag(err, stdout, stderr)
{
	if (err) {
        console.error('egret publish error:' + err);
    }
    else 
    {
        console.log(stdout);
        if(stdout.indexOf(tag_name) > -1)//是否存在这个标签
        {
        	cmdStr = "git checkout " + tag_name;
        	exec(cmdStr, tag_checkout);
        }
        else
        {
        	console.warn("Do not have this tag :" + tag_name);
        }
    }
}

function tag_checkout(err, stdout, stderr)
{
	if (err) {
        console.error('egret publish error:' + err);
    }
    else 
    {
        console.log(stdout);
        cmdStr = "git rev-parse HEAD";
		exec(cmdStr, get_tag_id);
		
	}
}

function get_egret_info(err, stdout, stderr)
{
	if (err) {
        console.error('get_egret_info error:' + err);
    }
    else 
    {
		console.log(stdout);
    	cmdStr = "cd GameHall/;ls;egret publish --version 1225";
		exec(cmdStr, egret_publish);
        console.log("egret_publish......");
	}
}

function get_tag_id(err, stdout, stderr)
{
	if (err) {
        console.error('get_tag_id error:' + err);
    }
    else 
    {
    	console.log(stdout);
    	commit_id = stdout;
		cmdStr = "egret info";
		exec(cmdStr, get_egret_info);
    }
}

function egret_publish(err, stdout, stderr)
{
	if (err) {
        console.error('egret publish error:' + err);
    }
    else 
    {
    	console.log(stdout);
		if(debug_type)
		{
			cmdStr = "cd GameHall/bin-release/web/1225/resource/plugin/;python hash_assets.py 0";
		}
		else
		{
			cmdStr = "cd GameHall/bin-release/web/1225/resource/plugin/;python hash_assets.py 1";
		}
    	exec(cmdStr, execute_py);
    }
}

function execute_py(err, stdout, stderr)
{
	if (err) {
        console.error('execute_py error:' + err);
    }
    else 
    {
    	console.log(stdout);
    	cmdStr = "cd GameHall/bin-release/web/1225;zip -q -r files.zip libs resource favicon.ico index.html index.manifest main.min.js cdn.html";//h5ar这个目录
    	exec(cmdStr, execute_zip);
        console.log("execute_zip......");
    }
}

function execute_zip(err, stdout, stderr)
{
	if (err) {
        console.error('execute_zip error:' + err);
    }
    else
    {
    	console.log(stdout);
        let obj = {};
        obj["commit_id"] = commit_id;
        let str = JSON.stringify(obj);
        str = str.replace("\\n","");
    	cmdStr = "rm -f output/files.zip;cp -f GameHall/bin-release/web/1225/files.zip output/;echo '" + str + "'> output/output.txt;git checkout -- .";//h5ar这个目录
        exec(cmdStr, succ);
    }
}

function succ(err, stdout, stderr)
{
    if (err) {
        console.error('egret publish error:' + stderr);
    }
    else
    {
        console.log(stdout);
        console.log("h5ar build success!!!");
    }
}